use strict;
use warnings;

package Lingua::EN::WSD::WebBased::Seeder::WordNetSense;

use base qw/Lingua::EN::WSD::WebBased::Seeder/;

use Lingua::EN::WSD::WebBased::Logger;
use Lingua::EN::WSD::WebBased::Utils;

use List::MoreUtils qw/uniq/;
use Perl6::Junction qw/any/;

use Class::AccessorMaker {
    wordnet             => undef,
    including_syns      => 1
}, 'new_init';

sub init {
    
    my $self = shift;

    $self->SUPER::init;

    check_type 'attribute wordnet'
        => $self->wordnet => 'WordNet::QueryData';

}

sub get_related_senses {

    my ($self, $sense, $semantic_link) = @_;

    my @related_senses =
        $semantic_link eq any(qw/syns hype hypo mero holo sim enta caus/) ?
            $self->wordnet->querySense($sense, $semantic_link) :
        $semantic_link eq any(qw/ants part pert attr/) ?
            $self->wordnet->queryWord($sense, $semantic_link) :
        _error("unknown semantic link: $semantic_link");

    return \@related_senses;

}

# generate seeds for a WordNet sense

sub gen_seeds {

    my ($self, $sense, $semantic_links) = @_;

    $semantic_links ||= [qw/syns hype hypo mero holo/];

    _trace(__PACKAGE__, "::gen_seeds($sense, [@$semantic_links])");

    my @related_senses = 
        map { @{$self->get_related_senses($sense, $_)} } @$semantic_links;
    @related_senses = 
        map { @{$self->get_related_senses($_, 'syns')} } @related_senses
            if $self->including_syns;

    my $core = $self->gen_query_core($sense);
    my @seeds = grep { $_ ne $core } 
        uniq map { s/_/ /g; /^(.*?)#/; $1 } @related_senses;
    @seeds = sort @seeds if $self->sort_seeds;

    return \@seeds;

}

sub gen_extra {

    my ($self, $sense) = @_;

    _trace(__PACKAGE__, ":gen_extra($sense)");

    return $self->wordnet->querySense($sense, 'glos');

}

sub gen_query_core {
    
    my ($self, $sense) = @_;
    return do { $sense =~ /^(.*?)#/; $1 };

}

1;
